package com.cz.nongyetongv5.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.bean.ArchiveBean;


public class ArchiveChargeFragment extends Fragment implements View.OnClickListener {

    private View view;
    private ArchiveBean archiveBean;
    private TextView tv_charge;
    private TextView tv_card;
    private TextView tv_sex;
    private TextView tv_edu;
    private TextView tv_tel;
    private TextView tv_mobile;
    private TextView tv_email;
    private TextView tv_qq;


    private LinearLayout ll_email;
    private LinearLayout ll_qq;


    private ImageView iv_tel;
    private ImageView iv_sms;
    private ImageView iv_mobile;


    public static ArchiveChargeFragment newInstance(ArchiveBean bean) {
        ArchiveChargeFragment fragment = new ArchiveChargeFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("bean", bean);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_archive_charge, null);
        archiveBean = (ArchiveBean) getArguments().getSerializable("bean");
        init();
        return view;
    }


    public void init() {
        tv_charge = view.findViewById(R.id.tv_charge);
        tv_card = view.findViewById(R.id.tv_card);
        tv_sex = view.findViewById(R.id.tv_sex);
        tv_edu = view.findViewById(R.id.tv_edu);
        tv_tel = view.findViewById(R.id.tv_tel);
        tv_mobile = view.findViewById(R.id.tv_mobile);
        tv_email = view.findViewById(R.id.tv_email);
        tv_qq = view.findViewById(R.id.tv_qq);


        ll_email = view.findViewById(R.id.ll_email);
        ll_qq = view.findViewById(R.id.ll_qq);

        iv_tel = view.findViewById(R.id.iv_tel);
        iv_sms = view.findViewById(R.id.iv_sms);
        iv_mobile = view.findViewById(R.id.iv_mobile);
        iv_tel.setOnClickListener(this);
        iv_sms.setOnClickListener(this);
        iv_mobile.setOnClickListener(this);
        if (archiveBean != null) {
            tv_charge.setText(archiveBean.getCharge());
            tv_card.setText(archiveBean.getIdcard());
            tv_sex.setText(archiveBean.getSex_name());
            tv_edu.setText(archiveBean.getEducation_name());
            tv_tel.setText(archiveBean.getTel());
            tv_mobile.setText(archiveBean.getMobile());
            if (archiveBean.getEmail() != null && !archiveBean.getEmail().equals("")) {
                ll_email.setVisibility(View.VISIBLE);
                tv_email.setText(archiveBean.getEmail());
            }
            if (archiveBean.getQq() != null && !archiveBean.getQq().isEmpty()) {
                ll_qq.setVisibility(View.VISIBLE);
                tv_qq.setText(archiveBean.getQq());
            }
            if (archiveBean.getTel()==null  || archiveBean.getTel().isEmpty()){
                iv_tel.setVisibility(View.GONE);
            }else {
                iv_tel.setVisibility(View.VISIBLE);
            }

            if (archiveBean.getMobile()==null  || archiveBean.getMobile().isEmpty()){
                iv_sms.setVisibility(View.GONE);
                iv_mobile.setVisibility(View.GONE);
            }else {
                iv_sms.setVisibility(View.VISIBLE);
                iv_mobile.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_tel:
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + archiveBean.getTel()));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                break;
            case R.id.iv_sms:
                Uri uri2 = Uri.parse("smsto:" + archiveBean.getMobile());
                Intent intentMessage = new Intent(Intent.ACTION_VIEW, uri2);
                startActivity(intentMessage);
                break;
            case R.id.iv_mobile:
                Intent intent1 = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + archiveBean.getMobile()));
                intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent1);
                break;
            default:
                break;
        }
    }
}